//
//  ShowOrdersFormViewController.swift
//  iModern
//
//  Created by Ralf on 8/11/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class ShowOrdersFormViewController: FormViewController
{

    var order = NSMutableDictionary()
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.initBackButton()
        self.initRightBarButtonItem()
        self.initializeForm()
        self.navigationItem.title = "订单详情"

    }

    func initRightBarButtonItem()
    {
        let button: UIButton = UIButton(type: UIButtonType.System) ;
        button.frame = CGRectMake(0, 0, 80, 30)
        button.setTitle("联系买家", forState: UIControlState.Normal)
        button.setTitleColor(UIColor(red: 0.40784313725490196, green: 0.6588235294117647, blue: 0.19215686274509805, alpha: 1), forState: UIControlState.Normal)
        button.addTarget(self, action: Selector("loadShareMenu:"), forControlEvents: UIControlEvents.TouchUpInside)
        let showButton = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = showButton
    }
    
    @IBAction func loadShareMenu(sender: AnyObject)
    {
        let alertContorller = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        let alertCancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
        let alertChat = UIAlertAction(title: "私聊", style: UIAlertActionStyle.Default,handler: chat)
        alertContorller.addAction(alertCancel)
        alertContorller.addAction(alertChat)
        self.presentViewController(alertContorller, animated: true, completion: nil)
    }
    
    func chat(alertAction:UIAlertAction)
    {
        let contact = self.order["contact"] as! [String:AnyObject]
        let controller  = ChatViewController()
        controller.contact = contact
        self.pushViewController(controller)
    }
    
    func initializeForm()
    {
        let form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor
        
        let orde        = order
        let advertising = orde["advertising"] as! [String:AnyObject]
        let contact     = advertising["contact"] as! [String:AnyObject]
        let review      = order["review"] as? String
        
        let price       = advertising["price"] as? Float
        let nub         = orde["quantity"] as? Float
        let totalPrices = price! * nub!
        
        form = XLFormDescriptor(title: "")
        section = XLFormSectionDescriptor.formSectionWithTitle("")
        form.addFormSection(section)
        
        if review == "null" || review == nil
        {
            row = XLFormRowDescriptor(tag: "evaluate", rowType: XLFormRowDescriptorTypeSelectorSegmentedControl, title: "未评价")
//            row.selectorOptions = ["评价"]
//            row.cellConfig["textLabel.color"] = UIColor.orangeColor()
//            row.cellConfigAtConfigure["textLabel.color"] = UIColor.redColor()
            section.addFormRow(row)
        }
        else
        {
            row = XLFormRowDescriptor(tag: "evaluate", rowType: XLFormRowDescriptorTypeText, title: "已评价")
            row.cellConfig["textLabel.color"] = UIColor.orangeColor()
            row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
            row.cellConfigAtConfigure["textLabel.color"] = UIColor.redColor()
            row.disabled = NSNumber(bool: true)
            section.addFormRow(row)
        }
        
        _ = self.order["contact"] as! NSDictionary
        row = XLFormRowDescriptor(tag: "alias", rowType: XLFormRowDescriptorTypeText, title: contact["fullName"] as? String)
        row.disabled = NSNumber(bool: true)
        row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
        row.value = contact["shopName"] as? String
        row.required = true
        section.addFormRow(row)
        
        let p = String((orde["price"] as? Int)!)
        row = XLFormRowDescriptor(tag: "quantity", rowType: XLFormRowDescriptorTypeText, title: advertising["summary"] as? String)
        row.disabled = NSNumber(bool: true)
        row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
        row.value = "￥" + p
        row.required = true
        section.addFormRow(row)
        
        let n = String((orde["quantity"] as? Int)!)
        row = XLFormRowDescriptor(tag: "totalPrices", rowType: XLFormRowDescriptorTypeText, title: "合计:")
        row.disabled = NSNumber(bool: true)
        row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
        row.value = n + "个   " + "  ￥" + String(stringInterpolationSegment: totalPrices)
        row.required = true
        section.addFormRow(row)
        
        section = XLFormSectionDescriptor.formSectionWithTitle("订单详情")
        form.addFormSection(section)
        
        row = XLFormRowDescriptor(tag: "address", rowType: XLFormRowDescriptorTypeText, title: "快递地址")
        row.disabled = NSNumber(bool: true)
        row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
        row.value = self.order["address"] as? String
        row.required = true
        section.addFormRow(row)
        
        let nameOfConsignee      = self.order["nameOfConsignee"] as? String
        let cellphoneOfConsignee = self.order["cellphoneOfConsignee"] as? String
        row = XLFormRowDescriptor(tag: "fullName", rowType: XLFormRowDescriptorTypeText, title: "收货人")
        row.disabled = NSNumber(bool: true)
        row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
        row.value = nameOfConsignee! + "  " + cellphoneOfConsignee!
        row.required = true
        section.addFormRow(row)
        
        section = XLFormSectionDescriptor.formSectionWithTitle("订单留言")
        form.addFormSection(section)
        
        row = XLFormRowDescriptor(tag: "message", rowType: XLFormRowDescriptorTypeTextView, title: "")
        row.disabled = NSNumber(bool: true)
        row.value = self.order["message"] as? String
        row.required = true
        
        section.addFormRow(row)
        
        let id = self.order["id"] as! Int
        section = XLFormSectionDescriptor.formSectionWithTitle("下单时间：")
        section.footerTitle = NSString(format: "订单号：%@", String(id)) as String
        form.addFormSection(section)
        
        section = XLFormSectionDescriptor.formSectionWithTitle("联系iModern爱魔登客服： 4000060155")//attributeString
        form.addFormSection(section)
        
        self.form = form
    }


    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
       
    }

}
